package org.skydiveapplibrary.ui;

import java.io.ByteArrayOutputStream;
import java.io.File;

import org.skydiveapplibrary.util.FileManager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;

public class ImageManager
{
	private Context mContext;
	private FileManager mFileManager;
	
	public ImageManager(Context context)
	{
		mContext = context;
		mFileManager = new FileManager(context);
	}
	
	public Bitmap decodeToScreenSize(byte[] data)
	{
		Point screenSize = getScreenSize();
		return decodeToSize(data, screenSize.x, screenSize.y);
	}
	
	public Bitmap decodeToSize(byte[] data, int maxWidth, int maxHeight)
	{
		// decode with inJustDecodeBounds=true to check dimensions
	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = true;
	    BitmapFactory.decodeByteArray(data, 0, data.length, options);

	    // Calculate inSampleSize
	    options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);

	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;
	    return BitmapFactory.decodeByteArray(data, 0, data.length, options);
	}
	
	public Bitmap decodeToScreenSize(String filename)
	{
		Point screenSize = getScreenSize();		
		return loadToSize(filename, screenSize.x, screenSize.y);
	}
	
	public Bitmap loadToSize(String filename, int maxWidth, int maxHeight)
	{
		File file = mFileManager.getStorageFile(filename);
		
		// decode with inJustDecodeBounds=true to check dimensions
	    BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = true;
	    BitmapFactory.decodeFile(file.getAbsolutePath(), options);

	    // Calculate inSampleSize
	    options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);

	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;
	    return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
	}
	
	public byte[] decodeContentUri(Uri contentUri)
	{
		byte[] byteArray = null;
		try
		{
			Bitmap bmp = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), contentUri);
			
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
			bmp.recycle();
			bmp = null;
			byteArray = stream.toByteArray();
			stream.close();
			stream = null;
		}
		catch (Exception ex)
		{
			Log.e("Error occurred parsing image", ex.getMessage());
		}	
		
		return byteArray;
	}
	
	public Point getScreenSize()
	{
		// get window size
		WindowManager windowMgr = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		Display display = windowMgr.getDefaultDisplay();
		
		@SuppressWarnings("deprecation")
		int width = display.getWidth();
		@SuppressWarnings("deprecation")
		int height = display.getHeight();
		
		Point screenSize = new Point(width, height);
		return screenSize;
	}
	
	private int calculateInSampleSize(
        BitmapFactory.Options options, int maxWidth, int maxHeight)
	{
		// Raw height and width of image
		int height = options.outHeight;
		int width = options.outWidth;
		int inSampleSize = 1;

		if (height > maxHeight || width > maxWidth)
		{
			if (width > height)
			{
				inSampleSize = Math.round((float) height / (float) maxHeight);
			}
			else
			{
				inSampleSize = Math.round((float) width / (float) maxWidth);
			}
		}
		return inSampleSize;
	}
	
	public int getSizeInBytes(Bitmap bitmap)
	{
        return bitmap.getRowBytes() * bitmap.getHeight();
	}
}
